/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JDialogListarUsuarios.java
 *
 * Created on Dec 22, 2010, 12:31:19 PM
 */
package br.com.tbp.gui;

import br.com.tbp.controle.ControleUsuario;
import br.com.tbp.exception.PersistenciaException;
import br.com.tbp.gui.table.UsuariosTableModel;
import br.com.tbp.model.domain.Usuario;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.JRootPane;
import javax.swing.KeyStroke;

/**
 *
 * @author Diego Freire
 */
public class UsuariosSemPadraoDialog extends javax.swing.JDialog {

    private UsuariosTableModel modeloDaTabela;

    public UsuariosSemPadraoDialog(java.awt.Frame parent, boolean modal) {
        super(parent, "TBP - Lista de Usuários", modal);
        initComponents();
        setLocationRelativeTo(null);

        try {
            refreshLista(new ControleUsuario().listar());
        } catch (PersistenciaException pex) {
            System.out.println(pex.getMessage());
        }
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabelTotalUsuarios = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        scrollpane = new javax.swing.JScrollPane();
        tabela = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        novoButton = new javax.swing.JButton();
        verDadosButton = new javax.swing.JButton();
        trocarSenhaButton = new javax.swing.JButton();
        fecharButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jLabelTotalUsuarios.setFont(new java.awt.Font("Dialog", 0, 12));
        jLabelTotalUsuarios.setText("jLabel1");
        jPanel1.add(jLabelTotalUsuarios);

        getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_START);

        jPanel2.setPreferredSize(new java.awt.Dimension(700, 350));
        jPanel2.setLayout(new java.awt.BorderLayout());

        tabela.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tabela.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelaMouseClicked(evt);
            }
        });
        scrollpane.setViewportView(tabela);

        jPanel2.add(scrollpane, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

        novoButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/tbp/resources/plus_16.png"))); // NOI18N
        novoButton.setMnemonic('n');
        novoButton.setText("Novo Usuário");
        novoButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                novoButtonActionPerformed(evt);
            }
        });
        jPanel3.add(novoButton);

        verDadosButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/tbp/resources/lapis16.png"))); // NOI18N
        verDadosButton.setMnemonic('a');
        verDadosButton.setText("Visualizar Dados");
        verDadosButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                verDadosButtonActionPerformed(evt);
            }
        });
        jPanel3.add(verDadosButton);

        trocarSenhaButton.setText("Trocar Senha");
        trocarSenhaButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/tbp/resources/chave16.png"))); // NOI18N
        trocarSenhaButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                trocarSenhaButtonActionPerformed(evt);
            }
        });
        jPanel3.add(trocarSenhaButton);

        fecharButton.setText("Fechar (ESC)");
        fecharButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fecharButtonActionPerformed(evt);
            }
        });
        jPanel3.add(fecharButton);

        getContentPane().add(jPanel3, java.awt.BorderLayout.PAGE_END);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void fecharButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fecharButtonActionPerformed
        dispose();
    }//GEN-LAST:event_fecharButtonActionPerformed

    private void novoButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_novoButtonActionPerformed
        UsuarioDialog dialog = new UsuarioDialog(null, true);
        dialog.setVisible(true);
    }//GEN-LAST:event_novoButtonActionPerformed

    private void verDadosButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_verDadosButtonActionPerformed
        visualizarDados();
    }//GEN-LAST:event_verDadosButtonActionPerformed

    private void tabelaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelaMouseClicked
        if (evt.getClickCount() == 2) {
            visualizarDados();
        }
    }//GEN-LAST:event_tabelaMouseClicked

    private void trocarSenhaButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_trocarSenhaButtonActionPerformed
        int row = tabela.getSelectedRow();
        if (row >= 0) {
            Usuario usuario = modeloDaTabela.getUsuario(row);
            if (usuario != null) {
                TrocarSenhaDialog dialog = new TrocarSenhaDialog(null, true, usuario.getUsuarioId());
                dialog.setVisible(true);
            }
        }
    }//GEN-LAST:event_trocarSenhaButtonActionPerformed

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                UsuariosSemPadraoDialog dialog = new UsuariosSemPadraoDialog(null, true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // <editor-fold defaultstate="collapsed" desc="Declaration of class members">
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton fecharButton;
    private javax.swing.JLabel jLabelTotalUsuarios;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JButton novoButton;
    private javax.swing.JScrollPane scrollpane;
    private javax.swing.JTable tabela;
    private javax.swing.JButton trocarSenhaButton;
    private javax.swing.JButton verDadosButton;
    // End of variables declaration//GEN-END:variables
    // </editor-fold>

    private void refreshLista(List<Usuario> lista) {
        modeloDaTabela = new UsuariosTableModel(lista);
        tabela.setModel(modeloDaTabela);
        jLabelTotalUsuarios.setText("Total de usuários cadastrados: " + lista.size());
    }

    private void visualizarDados() {
        int row = tabela.getSelectedRow();
        if (row >= 0) {
            Usuario usuario = modeloDaTabela.getUsuario(row);
            if (usuario != null) {
                UsuarioDialog dialog = new UsuarioDialog(null, true, usuario.getUsuarioId());
                dialog.setVisible(true);
            }
        }
    }

    @Override
    protected JRootPane createRootPane() {
        ActionListener closeListener = new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                fecharButtonActionPerformed(e);
            }
        };
        KeyStroke stroke =
                KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
        JRootPane myRootPane = new JRootPane();
        myRootPane.registerKeyboardAction(
                closeListener, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);

        return myRootPane;
    }
}
